package com.sctvcloud.qingchuan.ui.adapter.holder;

import android.content.Context;
import android.text.TextUtils;
import android.util.Pair;
import android.view.View;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.gridsum.tracker.GridsumWebDissector;
import com.ruihang.generalibrary.ui.widget.CustomFontTextView;
import com.ruihang.generalibrary.utils.DateUtils;
import com.sctvcloud.qingchuan.R;
import com.sctvcloud.qingchuan.beans.AccountNewsItem;
import com.sctvcloud.qingchuan.ui.utils.GlideUtil;

import java.util.ArrayList;
import java.util.HashMap;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * 关注  融媒体新闻
 */
public class AttentionNewsHolder extends BaseAbsHolder<AccountNewsItem> implements View.OnClickListener {

    @BindView(R.id.normal_news_layout)
    protected RelativeLayout normal_news_layout;
    @BindView(R.id.new_img)
    protected ImageView new_img;
    @BindView(R.id.img_num)
    protected TextView img_num;
    @BindView(R.id.news_title)
    protected CustomFontTextView news_title;
    @BindView(R.id.news_time)
    protected CustomFontTextView news_time;
    @BindView(R.id.news_time_author)
    protected CustomFontTextView news_time_author;

    @BindView(R.id.no_img_news_layout)
    protected RelativeLayout no_img_news_layout;
    @BindView(R.id.no_img_news_title)
    protected CustomFontTextView no_img_news_title;
    @BindView(R.id.no_img_news_time)
    protected CustomFontTextView no_img_news_time;
    @BindView(R.id.no_img_news_author)
    protected CustomFontTextView no_img_news_author;

    @BindView(R.id.video_news_layout)
    protected RelativeLayout video_news_layout;
    @BindView(R.id.video_news_title)
    protected CustomFontTextView video_news_title;
    @BindView(R.id.video_img)
    protected ImageView video_img;
    @BindView(R.id.video_news_time)
    protected CustomFontTextView video_news_time;
    @BindView(R.id.video_news_time_author)
    protected CustomFontTextView video_news_time_author;
    private OnNewsClickListener listener;
    private AccountNewsItem temp;
    private boolean isShowAuthor;

    public AttentionNewsHolder(Context context, View itemView, OnNewsClickListener listener, boolean isShowAuthor) {
        super(context, itemView);
        itemView.setOnClickListener(this);
        this.listener = listener;
        this.isShowAuthor = isShowAuthor;
    }


    @Override
    protected void initInject() {
        ButterKnife.bind(this, itemView);
    }

    @Override
    public void setData(AccountNewsItem t) {
        temp = t;
        if (TextUtils.isEmpty(t.getDataImg())) {
            //无图
            normal_news_layout.setVisibility(View.GONE);
            video_news_layout.setVisibility(View.GONE);
            no_img_news_layout.setVisibility(View.VISIBLE);

            no_img_news_title.setText(t.getTitle());
            String timeStr = DateUtils.formatDate(DateUtils.getDataDate(t), "");
            no_img_news_time.setText(timeStr);
            if (isShowAuthor) {
                no_img_news_author.setText(showAuthor(t.getAuthor()));
                no_img_news_author.setVisibility(View.VISIBLE);
            } else {
                no_img_news_author.setVisibility(View.GONE);
            }
        } else if (temp.getType() == 5) {
            //视频
            normal_news_layout.setVisibility(View.GONE);
            video_news_layout.setVisibility(View.VISIBLE);
            no_img_news_layout.setVisibility(View.GONE);
            GlideUtil.getGlideWithSmall169DefCenterCrop(context, t.getDataImg()).into(video_img);
            video_news_title.setText(t.getTitle());
            String timeStr = DateUtils.formatDate(DateUtils.getDataDate(t), "");//DateUtils.getDateHourString(DateUtils.getDataDate(t));
            video_news_time.setText(timeStr);
            if (isShowAuthor) {
                video_news_time_author.setText(showAuthor(t.getAuthor()));
                video_news_time_author.setVisibility(View.VISIBLE);
            } else {
                video_news_time_author.setVisibility(View.GONE);
            }
        } else if (temp.getType() == 2) {
            //组图
            normal_news_layout.setVisibility(View.VISIBLE);
            video_news_layout.setVisibility(View.GONE);
            no_img_news_layout.setVisibility(View.GONE);
            img_num.setText("" + t.getImgCount() + " 图");
            img_num.setVisibility(View.VISIBLE);
            news_title.setText(t.getTitle());
            String timeStr = DateUtils.formatDate(DateUtils.getDataDate(t), "");
            news_time.setText(timeStr);
            if (isShowAuthor) {
                news_time_author.setText(showAuthor(t.getAuthor()));
                news_time_author.setVisibility(View.VISIBLE);
            } else {
                news_time_author.setVisibility(View.GONE);
            }
            GlideUtil.getGlideWithSmall169DefCenterCrop(context, t.getDataImg()).into(new_img);
        } else {
            //有图新闻
            normal_news_layout.setVisibility(View.VISIBLE);
            video_news_layout.setVisibility(View.GONE);
            no_img_news_layout.setVisibility(View.GONE);
            img_num.setVisibility(View.GONE);
            news_title.setText(t.getTitle());
            String timeStr = DateUtils.formatDate(DateUtils.getDataDate(t), "");
            news_time.setText(timeStr);
            if (isShowAuthor) {
                news_time_author.setText(showAuthor(t.getAuthor()));
                news_time_author.setVisibility(View.VISIBLE);
            } else {
                news_time_author.setVisibility(View.GONE);
            }
            GlideUtil.getGlideWithSmall169DefCenterCrop(context, t.getDataImg()).into(new_img);
        }

    }

    private String showAuthor(String author) {
        String str;
        if (author.length() > 6) {
            author = author.substring(0, 6) + "...";
        }
        str = author;

        return str;
    }

    @Override
    public void setItemMode(int mode) {
        super.setItemMode(mode);

    }

    @Override
    public void onDestory() {
        super.onDestory();
        if (itemView != null) {
            itemView.setOnClickListener(null);
        }
    }

    @Override
    public void onClick(View v) {
        if (temp == null) {
            Toast.makeText(context, "数据错误", Toast.LENGTH_SHORT).show();
            return;
        }
        ArrayList<Pair<String, String>> pairs = new ArrayList<>();
        pairs.add(new Pair<>("页面", "首页"));
        pairs.add(new Pair<>("栏目", "精选"));
        pairs.add(new Pair<>("位置", "24小时"));
        ArrayList<Pair<String, String>> tempPair = new ArrayList<>();
        tempPair.addAll(pairs);
        tempPair.add(new Pair<>("行为类型", "点击"));

        if (activity != null) {
            GridsumWebDissector.getInstance().trackEvent(activity, "", temp.getTitle(), "", 200, tempPair);
        } else if (fragment != null) {
            GridsumWebDissector.getInstance().trackEvent(fragment, "", temp.getTitle(), "", 200, tempPair);
        }
        HashMap<String, String> map = new HashMap<>();
        for (Pair<String, String> p : pairs) {
            map.put(p.first, p.second);
        }
        if (null != listener) {
            listener.OnItemClick(temp);
        }

    }

    public interface OnNewsClickListener {
        void OnItemClick(AccountNewsItem temp);
    }
}
